<style>
.wf_results_table td {
  padding: 0px;
}
</style>

<%= render :partial => "/tr8n/admin/common/header" %>
<%= render :partial => "tabs" %>

<%= tr8n_will_filter(@results) %>

<%=link_to_function("+ Add New Language", "Tr8n.UI.Lightbox.show('/tr8n/admin/language/lb_update', {height:400})")%>
 | 
<%=link_to_function("Calculate Completeness", 'calculateCompleteness()')%>
<br>

<%= form_tag("", :id => "languages_form") do %>
	<%= will_filter_table_tag(@results, :columns => [
    [:flag, lambda{|lang| image_tag("tr8n/flags/#{lang.flag}.png")}, :style => 'width:20px;', :title => ''],
    [:checkbox, 'languages'],
	  [:locale, lambda{|lang|
	    html = []
	    if lang.enabled?
	      html << '<span style="color:green">'
	    else  
	      html << '<span style="color:red">'
	    end
	    html << lang.locale
	    html << '</span>'
	    html.join(' ').html_safe
	  }],
	  [:english_name, lambda{|lang| link_to(lang.english_name, :action => :view, :lang_id => lang.id)}],
	  [:native_name, lambda{|lang| lang.native_name}],
	  [:right_to_left, lambda{|lang|
	    html = []
	    if lang.right_to_left?
	      html << '<span style="color:red">'
	    else  
	      html << '<span style="color:green">'
	    end
	    html << lang.right_to_left?
	    html << '</span>'
	    html.join(' ').html_safe
	  }, :filterable => true],
	  [:featured_index, lambda{|lang| lang.featured_index}, 'text-align:right;'],
	  [:users, lambda{|lang| lang.total_metric.user_count}, 'text-align:right;'], 
	  [:translators, lambda{|lang| lang.total_metric.translator_count}, 'text-align:right;'], 
	  [:translations, lambda{|lang| lang.total_metric.translation_count}, 'text-align:right;'], 
	  [:completeness, lambda{|lang|
	    html = []
	    completeness = lang.completeness ? lang.completeness : 0
	    if completeness > 90
	      html << '<span style="color:green">'
	    elsif completeness > 60 
	      html << '<span style="color:yellow">'
	    else  
	      html << '<span style="color:red">'
	    end
	    html << "#{completeness}%"
	    html << '</span>'
	    html.join(' ').html_safe
	  }, 'text-align:right;'],
	  [:actions, lambda{|lang|
	     html = [] 
	     html << link_to_function("Update", "Tr8n.UI.Lightbox.show('/tr8n/admin/language/lb_update?lang_id=#{lang.id}', {height:400})")
	     unless lang.default?
	      if lang.enabled?
	        html << link_to("Disable", :action => :disable, :lang_id => lang.id)
	      else
	        html << link_to("Enable", :action => :enable, :lang_id => lang.id) 
	      end
	     end 
	     html.join(' | ').html_safe 
	  }, 'text-align:right;white-space:nowrap;']
	]) %>
<% end %>

<%= will_filter_actions_bar_tag(@results, [
	['Enable Selected Languages', 'enableSelectedLanguages()'], 
	['Disable Selected Languages', 'disableSelectedLanguages()'],
	['Add To Component', 'addSelectedLanguagesToComponent()'],
]) %>

<script>
	function calculateCompleteness() {
		if (!confirm('Are you sure you want to run this opperation?'))
		   return;
			    
	  location = '/tr8n/admin/language/calculate_total_metrics'	
	}
	
  function disableSelectedLanguages() {
    if (!confirm("Are you sure you want to disable those languages?")) return;
    
    Tr8n.element('languages_form').action = "/tr8n/admin/language/disable";
    Tr8n.element('languages_form').submit();
  }

  function enableSelectedLanguages() {
    if (!confirm("Are you sure you want to enable those languages?")) return;
    
    Tr8n.element('languages_form').action = "/tr8n/admin/language/enable";
    Tr8n.element('languages_form').submit();
  }

  function getSelectedLanguages() {
    var i=0;
    var checkbox = Tr8n.element('wf_check_' + i);
    var languages = [];
    while (checkbox) {
      if (checkbox.checked) {
        languages.push(checkbox.value);
      }
      checkbox = Tr8n.element('wf_check_' + (++i));
    }
    return languages; 
  }

  function addSelectedLanguagesToComponent() {
    var languages = getSelectedLanguages();
    if (languages.length == 0) {
      alert('At least one translator must be selected.');
      return;
    }
    Tr8n.UI.Lightbox.show('/tr8n/admin/applications/lb_add_to_component?languages=' + languages, {height:400});
  }    
</script>

<%= render :partial => "/tr8n/admin/common/footer" %>
